package com.base.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.base.comm.CommContent;
import com.base.po.voip.WeeksAdv;
import com.base.service.LogsService;
import com.base.service.WeeksAdvService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * 添加签到广告信息服务端
 * 
 * @author f
 *
 */
@Controller
@RequestMapping("/admin")
public class add_WeeksAdv {

	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private WeeksAdvService weeksAdvService; // 签到广告信息

	// 添加签到广告信息
	@RequestMapping(value = "/toAddWeeksAdv", method = RequestMethod.POST)
	public @ResponseBody int toAddWeeksAdv(HttpServletRequest request, WeeksAdv weeksadv)
			throws IOException, ParseException {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		if (userid == null) return -1; // 未登录
		List<WeeksAdv> beanAds = weeksAdvService.selectByAll(new WeeksAdv(userid, (byte) 1)); // 获取签到广告
		if (beanAds != null && beanAds.size() > 0) return -2; // 只能添加一张 失败
		weeksadv.setId(Produce.getUUID()); // 设置参数 UUID
		weeksadv.setCreatetime(Produce.getNowDate()); // 创建时间
		weeksadv.setAgentid(userid); // 代理商ID
		weeksadv.setDatastatus((byte) 1); // 状态可用
		logsService.insert(CommContent.TYPE_CREATE, CommContent.LOGS_XIAOXI + "配置签到广告信息",
				(String) request.getSession().getAttribute("userNick"), true, IP.getIpAddr(request));
		return weeksAdvService.insertSelective(weeksadv); // 添加
	}
}
